/**
 * Created by kevin on 2017/07/12.
 *
 * 弹窗，一张图片，支持滚动
 *"id": "popuplayer_1_close_bn",
 *"src": "imgs/close.png?v="+version,
 *"group_id": "popuplayer_1",
 *"prop":{"scale":比率,"offset":{"x":比率,"y":比率},"align":"BM"},比率＝屏幕尺寸的
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function PopupSinglePic(config) {

        this.Container_constructor();
        this.config = config;
        this.init();
        //console.log(this.father.targetWidth);

    }
    //PopupLayer.TYPE = "people";


    var p = createjs.extend(PopupSinglePic, createjs.Container);

    p.className = "PopupSinglePic";

    //配置
    p.config;
    //背景遮罩
    p.bg;

    //holder，可视区域
    p.holder;

    // public methods:
    p.init = function () {
        this.drawPopup();
        //console.log(this.className);
    }

    p.onClick = function(evt){
        this.parent.removeChild(this);
        //console.log(this.parent.removeChild(this))
    }

    /*
     * 绘制popup
     * */
    p.drawPopup = function(){
        //console.log("popup")
        var config_bg;
        var config_close;
        for(var i = 0 ;i<this.config.length;i++){
            switch (this.config[i].id){
                case this.config[i].group_id+"_bg":
                    config_bg = this.config[i];
                    break;
                case this.config[i].group_id+"_close_bn":
                    config_close =  this.config[i];
                    break;
            }

        }

        this.bg = new createjs.Shape();
        this.bg.graphics.beginFill(config_bg.bg.color).drawRect(0,0,targetWidth,targetHeight);
        this.bg.alpha = 0;
        this.addChild(this.bg);
        //this.bg.on("click",this.onClick,this);

        this.holder = new createjs.Container();
        var bg = new createjs.Shape();
        bg.graphics.beginFill(config_bg.bg.color).drawRect(0,0,targetWidth*config_bg.c_holder.w,targetHeight*config_bg.c_holder.h);
        bg.alpha = config_bg.bg.alpha;
        this.holder.addChild(bg);
        //console.log(config_bg.c_holder.align);
        switch (config_bg.c_holder.align){
            case "TL":
                this.holder.x = 0;
                this.holder.y = 0;
                break;
            case "TR":
                this.holder.x = (targetWidth * (1 - config_bg.c_holder.w));
                this.holder.y = 0;
                break;
            case "TM":
                this.holder.x = (targetWidth * (1 - config_bg.c_holder.w))*.5;
                this.holder.y = 0;
                break;
            case "BL":
                this.holder.x = 0;
                this.holder.y = (targetHeight *(1 - config_bg.c_holder.h));
                break;
            case "BR":
                this.holder.x = (targetWidth * (1 - config_bg.c_holder.w));
                this.holder.y = (targetHeight *(1 - config_bg.c_holder.h));
                break;
            case "BM":
                this.holder.x = (targetWidth * (1 - config_bg.c_holder.w))*.5;
                this.holder.y = (targetHeight *(1 - config_bg.c_holder.h));
                break;
            case "MM":
                this.holder.x = (targetWidth * (1 - config_bg.c_holder.w))*.5;
                this.holder.y = (targetHeight *(1 - config_bg.c_holder.h))*.5;
                break;
            default :
                this.holder.x = (targetWidth * (1 - config_bg.c_holder.w))*.5;
                this.holder.y = (targetHeight *(1 - config_bg.c_holder.h))*.5;
                break;
        }
        this.holder.x += config_bg.c_holder.offset.x * targetWidth;
        this.holder.y += config_bg.c_holder.offset.y * targetHeight;

        this.addChild(this.holder);

        var bgPic = popupLayer.loader.getResult(config_bg.id);
        var pic = new createjs.Bitmap(bgPic);
        var obj_mask = {"w":config_bg.c_holder.w*targetWidth,"h":config_bg.c_holder.h*targetHeight};
        var fit = {"type":"exact_fit"};
        var scroller = new eosi.website.Scroller(pic,obj_mask,fit);
        this.holder.addChild(scroller);

        var closePic = popupLayer.loader.getResult(config_close.id);

        var close = new eosi.website.BaseButton();
        var close_bimmap = new createjs.Bitmap(closePic);
        close.addChild(close_bimmap);
        var real_scale = config_close.prop.scale*targetWidth/closePic.width;
        //console.log(config_close.prop.scale,closePic.width/targetWidth)
        close_bimmap.setTransform(0,0,real_scale,real_scale);
        close_bimmap.x = -closePic.width*close_bimmap.scaleX*.5;
        close_bimmap.y = -closePic.height*close_bimmap.scaleY*.5;

        this.holder.addChild(close);

        switch (config_close.prop.align){
            case "TL":
                close.x = 0;
                close.y = 0;
                break;
            case "TR":
                close.x = targetWidth*config_bg.c_holder.w;
                close.y = 0;
                break;
            case "TM":
                close.x = targetWidth*config_bg.c_holder.w*0.5;
                close.y = 0;
                break;
            case "BL":
                close.x = 0;
                close.y = targetHeight*config_bg.c_holder.h;
                break;
            case "BR":
                close.x = targetWidth*config_bg.c_holder.w;
                close.y = targetHeight*config_bg.c_holder.h;
                break;
            case "BM":
                close.x = targetWidth*config_bg.c_holder.w*.5;
                close.y = targetHeight*config_bg.c_holder.h;
                break;
        }
        close.x+=config_close.prop.offset.x * targetWidth;
        close.y+=config_close.prop.offset.y * targetHeight;
        close.on("click",this.onClick,this);

    }




    // add MyApp to myNameSpace
    window.eosi.website.PopupSinglePic = createjs.promote(PopupSinglePic, "Container");
//}());
}(window));





